﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Web.Configuration;

namespace ForestryEnterprise
{
    public partial class AddEquipments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            updateDataView();
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            string statement = "select eq_name, eq_status, eq_used, eq_type, eq_date from Equipment where eq_name=@eqName and eq_status=@eqStatus and eq_used=@eqUsed and eq_type=@eqType and eq_date=@eqDate";
            OleDbConnection connection = new OleDbConnection(WebConfigurationManager.ConnectionStrings["forestryDB"].ConnectionString);
            OleDbCommand command = new OleDbCommand(statement, connection);
            command.Parameters.AddWithValue("@eqName", this.EqName.Text);
            command.Parameters.AddWithValue("@eqStatus", this.EqStatus.SelectedValue);
            command.Parameters.AddWithValue("@eqUsed", this.EqUsed.SelectedValue);
            command.Parameters.AddWithValue("@eqType", this.EqType.Text);
            command.Parameters.AddWithValue("@eqDate", this.Calendar1.SelectedDate);
            connection.Open();

            if (command.ExecuteReader().HasRows)
            {
                this.err.Text = "Equipment exists";
                connection.Close();
                return;
            }
            connection.Close();

           statement = "insert into Equipment(eq_name, eq_status, eq_used, eq_type, eq_date) values (@eqName, @eqStatus, @eqUsed, @eqType, @eqDate)";
           
            command = new OleDbCommand(statement, connection);            
            command.Parameters.AddWithValue("@eqName", this.EqName.Text);
            command.Parameters.AddWithValue("@eqStatus", this.EqStatus.SelectedValue);
            command.Parameters.AddWithValue("@eqUsed", this.EqUsed.SelectedValue);
            command.Parameters.AddWithValue("@eqType", this.EqType.Text);
            command.Parameters.AddWithValue("@eqDate", this.Calendar1.SelectedDate);
            
            connection.Open();
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                err.Text = ex.Message;
            }

            connection.Close();
        }

        protected void EquipDataView_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.updateName.Text = this.EquipDataView.SelectedRow.Cells[2].Text;
            this.updateStatus.Text = this.EquipDataView.SelectedRow.Cells[3].Text;
            this.updateUsed.Text = this.EquipDataView.SelectedRow.Cells[4].Text;
            this.updateType.Text = this.EquipDataView.SelectedRow.Cells[5].Text;
            this.updateDate.Text = this.EquipDataView.SelectedRow.Cells[6].Text;
            this.updateItem.Visible = true;
        }

        private void updateDataView()
        {
            string statement = "select * from equipment";
            this.EquipData.SelectCommand = statement;
            this.EquipDataView.DataBind();
        }

        protected void cancel_Click(object sender, EventArgs e)
        {
            this.updateItem.Visible = false;
        }

        protected void update_Click(object sender, EventArgs e)
        {
            string statement = "update Equipment set eq_name=@eqName, eq_status=@eqStatus, eq_used=@eqUsed, eq_type=@eqType, eq_date=@eqDate where ";
            statement += "eq_id = " + this.EquipDataView.SelectedRow.Cells[1].Text;
            OleDbConnection connection = new OleDbConnection(WebConfigurationManager.ConnectionStrings["forestryDB"].ConnectionString);
            OleDbCommand command = new OleDbCommand(statement, connection);
            command.Parameters.AddWithValue("@eqName", this.updateName.Text);
            command.Parameters.AddWithValue("@eqStatus", this.updateStatus.Text);
            command.Parameters.AddWithValue("@eqUsed", this.updateUsed.Text);
            command.Parameters.AddWithValue("@eqType", this.updateType.Text);
            command.Parameters.AddWithValue("@eqDate", this.updateDate.Text);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
            this.updateItem.Visible = false;
            updateDataView();
        }

        
    }
}